﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Controls_Common_TableSortSearchPagination : UserControl
{
    #region private properties
    private bool _enablePaging = true;
    private bool _enableSorting = true;
    private bool _enableSearching = true;
    private bool _showInfoMessage = true;
    private bool _isTargetServerControl = true;
    private string _targetControlId;
    #endregion


    #region public properties

    [Browsable(true)]
    public bool EnablePaging
    {
        get { return _enablePaging; }
        set { _enablePaging = value; }
    }

    [Browsable(true)]
    public bool EnableSorting
    {
        get { return _enableSorting; }
        set { _enableSorting = value; }
    }

    [Browsable(true)]
    public bool EnableSearching
    {
        get { return _enableSearching; }
        set { _enableSearching = value; }
    }

    [Browsable(true)]
    public bool ShowInfoMessage
    {
        get { return _showInfoMessage; }
        set { _showInfoMessage = value; }
    }

    [Browsable(true)]
    public string TargetControlID
    {
        get
        {
            if (string.IsNullOrEmpty(_targetControlId))
                throw new NullReferenceException(
                    "You must bind to a control through the TargetControlId property before you use the Control");
            return _targetControlId;
        }
        set { _targetControlId = value; }
    }

    [Browsable(true)]
    public string NumberOfRowsCsv { get; set; }

    [Browsable(true)]
    public bool IsTargetServerControl
    {
        get { return _isTargetServerControl; }
        set { _isTargetServerControl = value; }
    }

    [Browsable(true)]
    public string ExcludeSortColumnCsv
    {
        get;
        set;
    }

    [Browsable(true)]
    public string EmptyDataText
    {
        get;
        set;
    }
    #endregion



    /// <summary>
    /// Recursive method for finding the child control relative to parent
    /// </summary>
    /// <param name="relativeTo"></param>
    /// <param name="sID"></param>
    /// <returns>Control</returns>
    private Control LocateControl(Control relativeTo, String sID)
    {
        Control _control;
        _control = relativeTo.FindControl(sID);
        if (_control == null && relativeTo.Parent != null)
            _control = LocateControl(relativeTo.Parent, sID);
        if (_control == null && relativeTo.Page != null)
            _control = relativeTo.Page.FindControl(sID);
        return _control;
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        StringBuilder script = new StringBuilder();
        script.Append("function customDataTable(){");
        script.Append("$(document).ready(function(){");
        if (IsTargetServerControl)
        {
            Control cntrl = null;
            if (!string.IsNullOrEmpty(TargetControlID))
            {
                cntrl = LocateControl(this, TargetControlID);
            }

            if (cntrl == null)
                throw new NullReferenceException(
                    "Control not found with an id = " + TargetControlID);
            if (cntrl.GetType().Equals(typeof(GridView)))
            {
                GridView grd = (GridView)cntrl;
                if (grd.HeaderRow != null)
                {
                    grd.CssClass += " display";
                    if (!EnableSorting)
                    {
                        grd.CssClass = grd.CssClass.Replace("display", string.Empty);
                    }
                    script.Append("$(\"#" + cntrl.ClientID + " tbody:first\").before(\"<thead></thead>\");");
                    script.Append("$(\"#" + cntrl.ClientID + " thead\").append($(" + "\"#" + cntrl.ClientID +
                                  " tr:first\"));");
                }
                //to show empty data text for grid
                if (grd.Rows.Count == 0)
                {
                    if (!string.IsNullOrEmpty(EmptyDataText))
                    {
                        lblEmptyData.Text = EmptyDataText;
                        lblEmptyData.Visible = true;
                    }
                    else lblEmptyData.Visible = false;
                }
                else lblEmptyData.Visible = false;

            }
            script.Append("$('#").Append(cntrl.ClientID).Append("').dataTable({");
        }
        else
            script.Append("$('#").Append(TargetControlID).Append("').dataTable({");

        //script.Append("\"aaSorting\": [[ 0, \"asc\" ]],");
        //script.Append("\"asSorting\": [ 'asc', 'desc' ],");
        script.Append("\"bPaginate\": " + EnablePaging.ToString().ToLower() + ",");
        script.Append("\"bFilter\": " + EnableSearching.ToString().ToLower() + ",");
        script.Append("\"bSort\": " + EnableSorting.ToString().ToLower() + ",");
        script.Append("\"bRetrieve\": true,");

        if (EnableSorting && !string.IsNullOrEmpty(ExcludeSortColumnCsv))
            script.Append("\"aoColumnDefs\": [{\"sClass\": \"reset-sort\", \"bSortable\": false,\"aTargets\":[" + ExcludeSortColumnCsv + "]}],");

        script.Append("\"bInfo\": " + ShowInfoMessage.ToString().ToLower() + ",");

        if (!string.IsNullOrEmpty(NumberOfRowsCsv)) script.Append("\"aLengthMenu\": [[" + NumberOfRowsCsv + ",-1],[" + NumberOfRowsCsv + ",'All']],");
        else
            script.Append("\"aLengthMenu\": [[10,25,50,100,-1], [10,25,50,100,'All']],");

        if (!EnablePaging && !EnableSearching && !EnableSorting && !ShowInfoMessage)
            script.Append("\"bJQueryUI\": false,");
        else
            script.Append("\"bJQueryUI\": true,");

        script.Append("\"sPaginationType\": \"full_numbers\"});");
        script.Append("});");
        script.Append("}");

        ScriptManager.RegisterClientScriptBlock(Page, GetType(), "CustomTable", script.ToString(), true);
    }
}